package org.dstart.air.window
{
	import flash.desktop.NativeApplication;
	import flash.desktop.SystemTrayIcon;
	import flash.display.NativeMenu;
	import flash.display.NativeMenuItem;
	import flash.display.NativeWindow;
	import flash.display.NativeWindowDisplayState;
	import flash.display.NativeWindowInitOptions;
	import flash.display.NativeWindowResize;
	import flash.display.NativeWindowSystemChrome;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.NativeWindowBoundsEvent;
	import flash.events.NativeWindowDisplayStateEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.system.Capabilities;
	import flash.utils.Timer;
	
	import org.dstart.air.window.events.WindowEvent;
	import org.dstart.air.window.sub.WindowBackground;

	public class SuperWindow extends NativeWindow
	{
		private var bgSprite:WindowBackground;	/** 窗口主体框架 **/	
		private var objSprite:Sprite;			/** 对象存放区 **/
		private var maskSprite:Sprite;			/** 遮照区 **/
		
		public function SuperWindow(ww:Number = 500, hh:Number = 400, xx:Number = -1, yy:Number = -1)
		{
			var tWin:NativeWindowInitOptions = new NativeWindowInitOptions();
			tWin.systemChrome = NativeWindowSystemChrome.NONE;
			tWin.transparent = true;
			
			super(tWin);

			this.activate();
			
			this.alwaysInFront = true;

			this.x = (xx < 0) ? (Capabilities.screenResolutionX - ww) / 2 : xx;
			this.y = (yy < 0) ? (Capabilities.screenResolutionY - hh) / 2 : yy;
			
			this.width = ww;
			this.height = hh;

			this.minSize = new Point(20, 20);
			this.maxSize = new Point(2000, 2000);

			this.visible = true;			
			
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			bgSprite = new WindowBackground();
			bgSprite.ChangeSize(ww, hh);
			
			objSprite = new Sprite();
			objSprite.x = 10;
			objSprite.y = 30;
			objSprite.cacheAsBitmap = true;
			
			maskSprite = new Sprite();
			maskSprite.x = maskSprite.y = 0;
			maskSprite.cacheAsBitmap = true;
			
			stage.addChild(bgSprite);
			stage.addChild(objSprite);
			stage.addChild(maskSprite);
			
			bgSprite.addEventListener(WindowBackground.CLOSE_WINDOWS, onCloseWindow);
			bgSprite.addEventListener(WindowBackground.DRAG_WINDOWS, onDragWindow);
			bgSprite.addEventListener(WindowBackground.RESIZE_WINDOWS, onResizeWindow);
			bgSprite.addEventListener(WindowBackground.MAX_WINDOWS, onMaxWindow);
			bgSprite.addEventListener(WindowBackground.MIN_WINDOWS, onMinWindow);
			bgSprite.addEventListener(WindowBackground.RETURN_WINDOWS, onReturnWindow);
			
			maskSprite.addEventListener(MouseEvent.CLICK, onClickMask);
			
			addEventListener(Event.RESIZE, onWindowResize);			
			addEventListener(NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGE, onDispChange);
		}
		
		/** 是否可用 **/
		public function set enabled(flag:Boolean):void
		{
			maskSprite.graphics.clear();
			
			if(!flag)
			{
				maskSprite.graphics.beginFill(0xFFFFFF, 1);
				maskSprite.graphics.drawRect(0, 0, bgSprite.width, bgSprite.height);
				maskSprite.graphics.endFill();
				
				maskSprite.alpha = 0;
			}
		}
		
		/** 重写标题函数 **/
		public override function set title(value:String):void
		{
			super.title = value;
			
			bgSprite.title = value;
		}	

		/****************************************************************************/

		/** 设置窗口属性 **/		
		protected function ChangeStype(isIcon:Boolean = true, 	/** 是否显示图标，默认为是 **/
									isResize:Boolean = true,	/** 是否允许修改大小，默认为允许 **/
									isMin:Boolean = true,		/** 是否允许最小化，默认为允许 **/
									isCanMove:Boolean = true,	/** 是否允许拖动，默认为允许 **/
									isCanClose:Boolean = true	/** 是否允许关闭窗口,默认为允许 **/
									):void
		{
			bgSprite.showIcon = isIcon;
			bgSprite.canResize = isResize;
			bgSprite.canMin = isMin;			
			bgSprite.canMove = isCanMove;	
			bgSprite.canClose = isCanClose;		
		}
		
		/****************************************************************************/
		
		private var t:Timer;		/** 闪动效果 **/
		
		private function onClickMask(e:MouseEvent):void
		{
			var isNew:Boolean = true;
			if(t)
			{
				if(t.running)
				{
					isNew = false;
				}
			}
			if(isNew)
			{
				t = new Timer(100, 6);
				t.addEventListener(TimerEvent.TIMER, onFlagMask);
				t.addEventListener(TimerEvent.TIMER_COMPLETE, onDelTimer);
				t.start();
			}
			
			this.dispatchEvent(new Event(WindowEvent.NOT_CLICK));
		}
		
		private function onFlagMask(e:TimerEvent):void
		{
			if(maskSprite.alpha == 0)
			{
				maskSprite.alpha = 0.3;
			}
			else
			{
				maskSprite.alpha = 0;
			}
		}
		
		private function onDelTimer(e:TimerEvent):void
		{
			maskSprite.alpha = 0;

			t.removeEventListener(TimerEvent.TIMER, onFlagMask);
			t.removeEventListener(TimerEvent.TIMER_COMPLETE, onDelTimer);			
		}
		
		private function onCloseWindow(e:Event):void
		{
			// this.close();
			NativeApplication.nativeApplication.exit(0);
		}
		
		private function onDragWindow(e:Event):void
		{
			startMove();
		}
		
		private function onResizeWindow(e:Event):void
		{
			startResize(NativeWindowResize.BOTTOM_RIGHT);
		}
		
		private function onMaxWindow(e:Event):void
		{
			maximize();
		}
	
		private function onMinWindow(e:Event):void
		{			
			minimize();
		}

		private function createSystrayRootMenu():NativeMenu 
		{
			var menu:NativeMenu = new NativeMenu();
			var openNativeMenuItem:NativeMenuItem = new NativeMenuItem("显示窗口");
			var exitNativeMenuItem:NativeMenuItem = new NativeMenuItem("退出");

			openNativeMenuItem.addEventListener(Event.SELECT, undock);
			exitNativeMenuItem.addEventListener(Event.SELECT, closeApp);

			menu.addItem(openNativeMenuItem);
			menu.addItem(new NativeMenuItem("",true));
			menu.addItem(exitNativeMenuItem);

			return menu;
		}

		private function dock():void 
		{			
			this.visible = false;
			
			NativeApplication.nativeApplication.icon.bitmaps = new Array(bgSprite.bitmap);
			
			SystemTrayIcon(NativeApplication.nativeApplication.icon).menu = createSystrayRootMenu();
			SystemTrayIcon(NativeApplication.nativeApplication.icon).tooltip = "点击右键弹出选项菜单";
			
			SystemTrayIcon(NativeApplication.nativeApplication.icon).addEventListener(MouseEvent.CLICK, undock);
		}
		
		private function undock(evt:Event):void 
		{
			NativeApplication.nativeApplication.icon.bitmaps = [];

			restore();
			
			this.visible = true;
			this.orderToFront();			
		}
		
		private function closeApp(evt:Event):void 
		{
			this.close();
		}
		
		private function onDispChange(e:NativeWindowDisplayStateEvent):void
		{
			if (e.afterDisplayState == NativeWindowDisplayState.MINIMIZED) 
			{
				e.preventDefault();		
				
				dock();
			}
			else if(e.afterDisplayState == NativeWindowDisplayState.NORMAL)
			{
				undock(e);
			}
		}
		
		private function onReturnWindow(e:Event):void
		{
			restore();
		}
		
		private function onWindowResize(e:NativeWindowBoundsEvent):void
		{			
			bgSprite.ChangeSize(e.afterBounds.width, e.afterBounds.height);
			
			DoResizeWindow();
		}		

		/****************************************************************************/

		/** 子类改写对象,用于在窗口大小改变时刷新时调用 **/
		protected function DoResizeWindow():void
		{	
		}
		
		/** 子类可调用对象,用于绘制新的对象 **/
		protected function get _root():Sprite
		{
			return objSprite;
		}
	}
}